package ztc.service;

import ztc.common.StuBean;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Set;

/**
 * Created by ztc on 16-4-6.下午7:12
 */
public class QRStatus {
    public static boolean isReflash(ServletContext application,HttpSession session){
        ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                (ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>) application.getAttribute("sessionID");
        ConcurrentHashMap<String,StuBean> sMap=
                aMap.get(session.getId());
       if(sMap==null||!sMap.containsKey(session.getAttribute("uuid"))||!sMap.get(session.getAttribute("uuid")).getName().equals("null")||session.getAttribute("uuid").equals("failed"))
            return true;
        else
            return false;
    }
    public static void rollback(ServletContext application,HttpSession session){
        ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                (ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>) application.getAttribute("sessionID");
        ConcurrentHashMap<String,StuBean> sMap=
                aMap.get(session.getId());
        sMap.remove(session.getAttribute("uuid"));
        session.setAttribute("uuid","failed");
        System.out.println("rollback");
    }
    public static void rmNull(ServletContext application,HttpSession session){
        ConcurrentHashMap<String,ConcurrentHashMap<String,StuBean>> aMap=
                (ConcurrentHashMap<String, ConcurrentHashMap<String, StuBean>>) application.getAttribute("sessionID");
        if(aMap!=null) {
            ConcurrentHashMap<String, StuBean> sMap =
                    aMap.get(session.getId());
            if(sMap!=null) {
                Set<String> set = sMap.keySet();
                for (Object s : set) {
                    if (sMap.get(s).getName().equals("null"))
                        sMap.remove(s);
                }
            }
        }
    }
}
